var global_web_admin_article_filter_is_delete = 'N';
var global_web_admin_article_filter_cat_id = 0;
var global_web_admin_article_filter_sub_cat_id = 0;
var global_web_admin_article_filter_key_word = '';
var global_web_admin_article_ui_list_id = 'contact';
var global_web_admin_article_total_page = 0;
var global_web_admin_article_current_page = 1;
$(function(){
	web_admin_js_refresh_list_data();
	$('.search-key-word').bind('keydown',function(e){
		var key = e.which;
        if (key == 13) {
            e.preventDefault();
            global_web_admin_article_filter_key_word = $(this).val();
            web_admin_js_refresh_list_data();
        }
	});
	$('.page_prev').bind('click',function(){
		global_web_admin_article_current_page -= 1;
		web_admin_js_refresh_list_data();
		return false;
	});
	$('.page_next').bind('click',function(){
		global_web_admin_article_current_page += 1;
		web_admin_js_refresh_list_data();
		return false;
	});
});
function web_admin_js_refresh_list_data()
{
	show_ajax_loader();
	var api_url = 'ajax.php?model=article&act=get_list_data&page='+global_web_admin_article_current_page;
	
	if ( global_web_admin_article_filter_is_delete != '' ){
		api_url += '&is_delete='+global_web_admin_article_filter_is_delete;
	}
	if ( global_web_admin_article_filter_cat_id > 0 ){
		api_url += '&cat_id='+global_web_admin_article_filter_cat_id;
	}
	if ( global_web_admin_article_filter_sub_cat_id > 0 ){
		api_url += '&sub_cat_id='+global_web_admin_article_filter_sub_cat_id;
	}
	if ( global_web_admin_article_filter_key_word != '') {
		api_url += '&key_word=' + encodeURIComponent(global_web_admin_article_filter_key_word);
	}
	$.get(api_url, function(data){
		  hide_ajax_loader();
		  if ( data.error_message != '') {
			  alert(data.error_message);
		  }else{
			  global_web_admin_article_total_page = data.data.total_page;
			  if ( global_web_admin_article_current_page > 1 ) {
				  $('.page_prev').show();
			  }else{
				  $('.page_prev').hide();
			  }
			  if ( global_web_admin_article_current_page < global_web_admin_article_total_page ) {
				  $('.page_next').show();
			  }else{
				  $('.page_next').hide();
			  }
				
			  $('#'+global_web_admin_article_ui_list_id+' ul:gt(0)').remove();
			  $.each(data.data.list_data,function(i, v){
				  var ui_element = '<ul id="'+v.id+'" class="row-data">';
				  ui_element += '<li class="peo-a">'+v.date+'</li>';
				  ui_element += '<li class="peo-b">'+v.sub_cat+'</li>';
				  ui_element += '<li class="peo-c">'+v.title+'</li>';
				  ui_element += '<li class="peo-d">'+v.times+'</li>';
				  ui_element += '<li class="peo-e">'; 
				  ui_element += '<a href="#" class="edit">修改</a>';
				  if ( v.is_delete == 'Y' ){
					  ui_element += '<a href="#" class="revokedelete" >回復</a>';
				  }else{
					  ui_element += '<a href="#" class="setdelete" >暫停</a>';
				  }
				
				  ui_element += '<a href="#" class="delete">刪除</a></li>';
				  ui_element += '</ul>';
				  $('#'+global_web_admin_article_ui_list_id).append(ui_element);
				  });
			  
			  $('#'+global_web_admin_article_ui_list_id+' ul.row-data li a').click(function(){
				  var ul_obj = $(this).closest('ul');
				  var id_val = ul_obj.attr('id');
				  var action = $(this).attr('class');
				  if ( action == 'edit'){
					  document.location = 'contact-add.php?act=edit&id='+id_val;
				  }
				  else if (action == 'setdelete') {
					  web_admin_js_set_delete(id_val);
				  }
				  else if (action == 'revokedelete') {
					  web_admin_js_revokedelete(id_val);
				  }
				  else if (action == 'delete') {
					  web_admin_js_delete(id_val);
				  }
				  return false;
			  });
		  }
		}, 'json');
	
}

function  web_admin_js_set_delete(id_val)
{
	show_ajax_loader();
	var api_url = 'ajax.php?model=article&act=set_delete&id='+id_val;
	$.get(api_url, function(data){
		hide_ajax_loader();
		web_admin_js_refresh_list_data();
	},'json');
}
function web_admin_js_revokedelete(id_val)
{
	show_ajax_loader();
	var api_url = 'ajax.php?model=article&act=revokedelete&id='+id_val;
	$.get(api_url, function(data){
		hide_ajax_loader();
		web_admin_js_refresh_list_data();
	},'json');
}
function web_admin_js_delete(id_val)
{
	if ( !confirm("確定刪除？") ) {
		return false;
	}
	show_ajax_loader();
	var api_url = 'ajax.php?model=article&act=delete&id='+id_val;
	$.get(api_url, function(data){
		hide_ajax_loader();
		web_admin_js_refresh_list_data();
	},'json');

}